<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\MemberLogOff;
use think\Db;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\PDOException;

/**
 * 注销记录管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/09/04
 * Class MemberLogOffService
 * @package app\admin\service
 */
class MemberLogOffService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new MemberLogOff();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2023/09/04
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();
        $page = $param['page'] ?? 1;
        $limit = $param['limit'] ?? 10;

        // 查询条件
        $map = [];
        if(isset($param['member_id']) && !empty($param['member_id'])){
            $map[] = ['member_id','=',$param['member_id']];
        }
        $list = $this->model->where($map)->page($page, $limit)->order('id desc')->select();
        foreach ($list as $k=>&$v){
            $v['member_account'] = get_substr_replace($v['member_account']);
            $v['phone'] = get_substr_replace($v['phone']);
        }
        $count = $this->model->where($map)->count();
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;

//        return parent::getListNew($map); // TODO: Change the autogenerated stub
    }

    /**
     * 设置状态
     * @param $userId   //审核员ID
     * @return array
     * @throws BindParamException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws PDOException|Exception
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function statusNew($userId): array
    {
        $data = request()->param();
        if (!$data['id']) {
            return message('记录ID不能为空', false);
        }
        if (!$data['status']) {
            return message('记录状态不能为空', false);
        }
        $error = '';
        $info = $this->model->getInfo($data['id']);
        $item = [
            'id'            => $data['id'],
            'audit_status'  => $data['status'],
            'audit_time'    => time(),
            'audit_admin_id'=> $userId
        ];
        if($data['status'] == 2){
            if($info['type'] == 1){
                $item['log_off_time'] = time() + (86400 * 7);
            }else{
//                $item['log_off_time'] = time() + (86400 * 2);
                $item['log_off_time'] = time();
//                $this->auditLogout($data['id'],$data['member_id'],$item);
//                return message();
            }
        }
        if($data['status'] == 3){
            $item['status'] = 3;
        }
        $rowId = $this->model->edit($item, $error);
        if (!$rowId) {
            return message($error, false);
        }
        return message();
    }

    /**
     * Desc : 及时注销
     * User : you name
     * Date : 2024-03-12 22:09
     * @param $id
     * @param $member_id
     * @param array $item
     * @throws PDOException
     * @throws Exception
     */
    public function auditLogout($id, $member_id, array &$item=[])
    {
        $save = array(
            'username'  => '',
            'password'  => '',
            'phone'     => '',
            'realname'  => '',
            'idcard'    => '',
            'age_status'=> 0,
            'log_off'   => 2,
            'mark'      => 0
        );
        $item['status'] = 2;
        Db::name('member')->where('id',$member_id)->update($save);
        Db::name('member_log_off')->where('id',$id)->update($item);
    }

                                                            
}